package zookeeper_tool;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;

import java.util.ArrayList;

public class CreateZookeeperNode
{

    public static void main(String[] args) throws Exception
    {

        int CONNECTION_TIMEOUT = 10;
        ZooKeeper zooKeeper = new ZooKeeper("192.168.1.106:2181",
                CONNECTION_TIMEOUT, new Watcher()
        {
            // 监控所有被触发的事件
            public void process(WatchedEvent watchedEvent)
            {
                System.out.println(watchedEvent);
            }
        });

        String path = "/drds7";
        ArrayList<ACL> aclList = new ArrayList<ACL>();
        ACL acl = new ACL(ZooDefs.Perms.ALL, new Id("digest", DigestAuthenticationProvider.generateDigest("drds:123456")));
        aclList.add(acl);
        zooKeeper.create(path, null, aclList, CreateMode.PERSISTENT);
        for (String string :
                zooKeeper.getChildren("/", null))
        {
            System.out.println(string);
        }
    }
}
